// pages/paperDetial/paperDetial.js
let api = require("../../../api/paperApi.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paper: {},
    totalQuestion: [], //所有题目
    currentIndex: 0, //当前做到的题目
    totalQuestionLength: 0, // 答案长度
    answerList:[], // 答案列表
    copyAnswerList:[],
    time: 0, // 答题时间
    timeShow: "00:00:00",
    answerSheet: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let paperId = options.paperId
    console.log(paperId)
    let that = this;
    api.getPaperDetial(paperId).then(res=>{
      console.log(res)
      let paper = res.data
      let totalQuestion = []
        for(let i = 0; i < paper.write_question.length; i ++){
          totalQuestion.push(paper.write_question[i])
        }
        for(let j = 0; j < paper.single_choice_question.length; j ++){
          totalQuestion.push(paper.single_choice_question[j])
        }
        for(let k = 0; k < paper.many_choice_question.length; k ++){
          totalQuestion.push(paper.many_choice_question[k])
        }
        that.setData({ totalQuestion: totalQuestion })
        that.setData({ totalQuestionLength: totalQuestion.length })
        that.setData({ paper: paper })
        that.initQuestionList()
        wx.hideLoading()
    })
    this.startTime()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  singleChoice:function(e){
    console.log(e)
    let attr = "answerList["+ e.currentTarget.dataset.index +"].answer"
    this.setData({ [attr]: e.currentTarget.dataset.answer })
    console.log(this.data.answerList)
  },

  manyChoice:function(e){
    console.log(e)
    let attr = "answerList["+ e.currentTarget.dataset.index +"].answer"
    let answer = this.data.answerList[e.currentTarget.dataset.index].answer
    let choice = e.currentTarget.dataset.answer
    if(this.contains(answer, choice)){
      //已经包含了 移除
      let index = -1;
      for(let i = 0; i < answer.length; i++){
        if(answer[i] == choice){
          index = i;
          break;
        }
      }
      answer.splice(index, 1)
      this.setData({ [attr]: answer })
    }else{
      //没有包含添加
      answer.push(choice)
      this.setData({ [attr]: answer })
    }
    console.log(this.data.answerList)
  },

  prevQuestion:function(){
    let index = this.data.currentIndex - 1;
    if(index < 0){
      wx.showToast({
        title: '这是第一题了',
        icon: 'none',
        duration: 2000
      })
    }else{
      this.setData({ currentIndex: index })
    }
  },

  nextQuestion:function(){
    let index = this.data.currentIndex + 1
    if(index >= this.data.totalQuestionLength){
      wx.showToast({
        title: '这是最后一题了',
        icon: 'none',
        duration: 2000
      })
    }else{
      this.setData({ currentIndex: index })
    }
    console.log(this.data.answerList[index])
  },

  initQuestionList:function(){
    let that = this;
    for(let i = 0; i < that.data.totalQuestion.length; i++){
      if(that.data.totalQuestion[i].type == "many"){
        let item = {
          "id": that.data.totalQuestion[i].id,
          "type": that.data.totalQuestion[i].type,
          "answer": []
        }
        that.data.answerList.push(item)
      }else{
        let item = { 
          "id": that.data.totalQuestion[i].id,
          "type": that.data.totalQuestion[i].type,
          "answer": ""
         }
         that.data.answerList.push(item)
      }
    }
    console.log(that.data.answerList)
  },

  writeAnswer:function(e){
    let attr = "answerList["+ e.target.dataset.index +"].answer"
    this.setData({ [attr]: e.detail.value })
  },


  contains:function(answer, choice){
    for(let i = 0; i < answer.length; i++){
      if(answer[i] == choice){
        return true
      }
    }
    return false;
  },

  submit:function(){
    let that = this;
    wx.showModal({
      title: "提示",
      content: "确认提交试卷吗？",
      success:function(res){
        if(res.cancel){
          //点击了取消
        }else{
          wx.setStorageSync('paper', that.data.paper)
          wx.setStorageSync('totalQuestion', that.data.totalQuestion)
          wx.setStorageSync('answerList', that.data.answerList)
          wx.navigateTo({
            url: '/pages/paper/paperAnswer/paperAnswer',
          })
        }
      }
    })
  },

  startTime:function(){
    let that = this;
    let interval = setInterval(function(){
      let time = that.data.time
      time += 1;
      let s = that.showNum(time % 60)
      let m = that.showNum(parseInt(time / 60 % 60))
      let h = that.showNum(parseInt(time / 60 / 60))
      that.setData({ timeShow: h + ":" + m + ":" + s })
      that.setData({ time: time })
    }, 1000)
  },

  showNum:function(num){
    if (num < 10) {
      return '0' + num
    }
    return num
  },

  toAnswerSheet:function(){
    this.setData({ answerSheet: true })
    console.log(this.data.answerList)
    let copy = []
    for(let i = 0; i < this.data.answerList.length; i++){
      copy.push(this.data.answerList[i])
    }
    this.setData({copyAnswerList:copy})
  },

  backQuestion:function(){
    this.setData({ answerSheet: false })
  },

  toQuestion:function(options){
     console.log(options)
     let index = options.currentTarget.dataset.index
     this.setData({ currentIndex: index })
     this.setData({ answerSheet: false })
  },

  favQuestion:function(){
    let _currentIndex = this.data.currentIndex
    console.log(this.data.totalQuestion[_currentIndex])
    // 判断类型 type：write single many
    
  }

})